package com.ktgj.service.common.ygcd;

import com.alibaba.fastjson.JSON;
import com.google.common.base.Strings;
import com.ktgj.service.common.ygcd.domain.request.*;
import com.ktgj.service.common.ygcd.domain.response.*;
import com.ktgj.service.common.ygcd.domain.util.Utils;

import java.io.IOException;

/** 阳光车导
 * @author wyj
 * @create 2021-10-21 10:21
 */
public class SunshineBus {
    //测试环境
    private static final String API_URL="http://beta.car.jryghq.com/";

    //线上环境
    /*private static final String API_URL="http://api.car.jryghq.com/";*/


    /**
     * 价格信息查询(日租、半日租服务)
     * @param request
     * @return
     */
    public DayPriceResponse _GetDayPrice(DayPriceRequest request){
        DayPriceResponse response = new DayPriceResponse();
        request.sign = Utils.getSign(request);
        String str = JSON.toJSONString(request);
        try {
            String result = Utils.postJson(API_URL + "Car/Price", str);
            response =JSON.parseObject(result,DayPriceResponse.class);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return response;
    }

    /**
     * 价格信息查询(点到点服务)
     * @param request
     * @return
     */
    public PriceResponse _GetPrice(PriceRequest request){
        PriceResponse response = new PriceResponse();
        request.sign = Utils.getSign(request);
        String strJson = JSON.toJSONString(request);
        try {
            String result = Utils.postJson(API_URL + "Car/Price", strJson);
            response =JSON.parseObject(result,PriceResponse.class);
        } catch (IOException e) {
            e.printStackTrace();
        }

        return response;
    }

    /**
     * 下单接口
     * @param request
     * @return
     */
    public CreateOrderResponse _CreateOrder(CreateOrderRequest request){
        CreateOrderResponse response = new CreateOrderResponse();

        try {
            request.sign = Utils.getSign(request);
            String strJson = JSON.toJSONString(request);
            String result = Utils.postJson(API_URL + "Car/CreateOrder", strJson);
            response =JSON.parseObject(result,CreateOrderResponse.class);
            if(!response.IsSucess){
                throw new Exception(response.ErrorMessage);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
            response.ErrorMessage = e.getMessage();
        }

        return response;
    }


    /**
     * 预取消接口
     * @param request
     * @return
     */
    public CancelOrderResponse _CancelOrder(CancelOrderRequest request){
        CancelOrderResponse response = new CancelOrderResponse();

        //约束条件判断
        request.cancelReason = Strings.isNullOrEmpty(request.cancelReason)? "其它原因" : request.cancelReason;

        //请求参数
        try {
            CancelOrderNewRequest requestNew = new CancelOrderNewRequest();
            requestNew.companyId = request.companyId;
            requestNew.cancelId = request.cancelId.getCode();
            requestNew.cancelReason = Strings.isNullOrEmpty(request.cancelReason) ? "其它原因" : request.cancelReason.trim();
            requestNew.orderCode = request.orderCode.trim();
            requestNew.vendorOrderCode = request.vendorOrderCode.trim();

            requestNew.sign = Utils.getSign(requestNew);
            String strJson = JSON.toJSONString(requestNew);

            String result = Utils.postJson(API_URL + "Car/CancelOrder", strJson);
            response =JSON.parseObject(result,CancelOrderResponse.class);

        } catch (IOException e) {
            e.printStackTrace();
        }

        return response;
    }

    /**
     * 取消确认接口
     * @param request
     * @return
     */
    public CancelOrderResponse _ConfirmCancelOrder(CancelOrderRequest request){
        CancelOrderResponse response = new CancelOrderResponse();

        //约束条件判断
        request.cancelReason = Strings.isNullOrEmpty(request.cancelReason)? "其它原因" : request.cancelReason;


        try {
            //请求参数
            CancelOrderNewRequest requestNew = new CancelOrderNewRequest();

            requestNew.companyId = request.companyId;
            requestNew.cancelId = request.cancelId.getCode();
            requestNew.cancelReason = Strings.isNullOrEmpty(request.cancelReason) ? "其它原因" : request.cancelReason.trim();
            requestNew.orderCode = request.orderCode.trim();
            requestNew.vendorOrderCode = request.vendorOrderCode.trim();

            requestNew.sign = Utils.getSign(requestNew);
            String strJson = JSON.toJSONString(requestNew);

            String result = Utils.postJson(API_URL + "Car/CancelOrderConfirm", strJson);
            response =JSON.parseObject(result,CancelOrderResponse.class);

        } catch (IOException e) {
            e.printStackTrace();
        }

        return response;
    }

    /**
     * 创建发票
     * @param request
     * @return
     */
    public CreateReceiptResponse _CreateReceipt(CreateReceiptRequest request){
        CreateReceiptResponse response = new CreateReceiptResponse();
        try {
            request.sign = Utils.getSign(request);
            String strJson = JSON.toJSONString(request);
            String result = Utils.postJson(API_URL + "Receipt/Create", strJson);
            response =JSON.parseObject(result,CreateReceiptResponse.class);
        } catch (IOException e) {
            e.printStackTrace();
        }

        return response;
    }

    /**
     * 订单查询
     * @param request
     * @return
     */
    public GetOrderDetailResponse GetOrderDetail(GetOrderDetailRequest request){
        GetOrderDetailResponse response = new GetOrderDetailResponse();
        try {
            request.sign = Utils.getSign(request);
            String strJson = JSON.toJSONString(request);
            String result = Utils.postJson(API_URL + "Car/OrderDetail", strJson);
            response =JSON.parseObject(result,GetOrderDetailResponse.class);
        } catch (IOException e) {
            e.printStackTrace();
        }

        return response;
    }

    /**
     * 订单状态变更接口
     * @param request
     * @return
     */
    public ChangeStatusResponse ChangeStatus(ChangeStatusRequest request){
        ChangeStatusResponse response = new ChangeStatusResponse();
        try {
            request.sign = Utils.getSign(request);
            String strJson = JSON.toJSONString(request);
            String result = Utils.postJson(API_URL + "Car/ChangeStatus", strJson);
            response =JSON.parseObject(result,ChangeStatusResponse.class);
        } catch (IOException e) {
            e.printStackTrace();
        }

        return response;
    }

    /*阳光车导回调API*/
    /**
     * 订单状态同步接口
     * @param request
     * @return
     */
    public RespBase OrderStatuCallBack(OrderStatuCallBackRequest request){
        RespBase response = new RespBase();
        if(request.sign.equals(Utils.getSign(request))){
            try {
                throw new Exception("验签错误");
            } catch (Exception e) {
                e.printStackTrace();
                response.Exception = e;
            }
        }
        return response;
    }

}
